<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员注册</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #000;
            color: #00ffcc;
        }

        .container {
            background-color: rgba(0, 0, 0, 0.8);
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 255, 204, 0.3);
            width: 350px;
            animation: fadeIn 0.5s ease-in-out;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #00ffcc;
        }

        input {
            width: 100%;
            padding: 12px;
            margin: 15px 0;
            border: none;
            border-radius: 5px;
            background-color: rgba(255, 255, 255, 0.1);
            color: #00ffcc;
            outline: none;
            transition: background-color 0.3s ease;
        }

        input:focus {
            background-color: rgba(255, 255, 255, 0.2);
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #00ffcc;
            color: #000;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #00ccaa;
        }

        p {
            text-align: center;
            margin-top: 20px;
        }

        a {
            color: #00ffcc;
            text-decoration: none;
            transition: color 0.3s ease;
        }

        a:hover {
            color: #00ccaa;
        }

        #error-message {
            color: red;
            text-align: center;
            margin-top: 10px;
        }
    </style>
</head>

<body>
<div class="container">
    <h2>管理员注册</h2>
    <form id="admin-register-form">
        <input type="text" id="admin-register-username" placeholder="用户名">
        <input type="password" id="admin-register-password" placeholder="密码">
        <button type="button" onclick="adminRegister()">注册</button>
    </form>
    <p>已有账号？<a href="login.html">登录</a></p>
    <div id="error-message"></div>
</div>

<script>
    const baseUrl = 'http://localhost:8080';

    async function adminRegister() {
        const username = document.getElementById('admin-register-username').value;
        const password = document.getElementById('admin-register-password').value;

        if (!username || !password) {
            alert('用户名和密码不能为空');
            return;
        }
        try {
            const response = await fetch(`${baseUrl}/admins/register`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password
                })
            });
            const result = await response.json();

            if (result.success) {
                alert('管理员注册成功，请登录！');
                window.location.href = 'login.html';
            } else {
                document.getElementById('error-message').textContent = result.message;
            }
        } catch (error) {
            console.error('注册请求出错', error);
            document.getElementById('error-message').textContent = '注册请求出错，请稍后再试';
        }
    }
</script>
</body>

</html>